import Vue from "vue";
import VueRouter from "vue-router";

// 引入view中的vue文件
import Login from "@/views/login";
import Article from "@/views/article";
import Dashboard from "@/views/dashboard";
import Layout from "@/views/layout";

import store from "@/store";
Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    { path: "/login", component: Login },
    {
      path: "/",
      component: Layout,
      redirect: "/dashboard",
      children: [
        { path: "dashboard", component: Dashboard },
        { path: "article", component: Article },
      ],
    },
  ],
});
// 白名单
const whiteList = ["/login"];
// 导航守卫           路由导航
router.beforeEach((to, from, next) => {
  /* must call `next` */
  if (store.state.user.token || whiteList.includes(to.path)) {
    next();
  } else {
    next("/login");
  }
});

export default router;
